package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.PortDao;
import com.bocloud.cmp.entity.resource.Port;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 端口DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("portDao")
public class PortDaoImpl extends BasicDao implements PortDao {

    @Override
    public List<Port> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from port where 1=1";
        sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
        Map<String, Object> paramMap = SQLHelper.getParam(params);
        List<Object> list = this.queryForList(sql, paramMap, Port.class);
        List<Port> ports = new ArrayList<>();
        Port port = null;
        for (Object obj : list) {
            port = (Port) obj;
            ports.add(port);
        }
        return ports;
    }

    @Override
    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name,a.port_id from port a where 1=1";
        sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
        Map<String, Object> paramMap = SQLHelper.getParam(params);
        List<Object> list = this.queryForList(sql, paramMap, Port.class);
        List<SimpleBean> beans = new ArrayList<>();
        Port port = null;
        for (Object obj : list) {
            port = (Port) obj;
            beans.add(new SimpleBean(port.getId(), port.getName(), port.getPortId()));
        }
        return beans;
    }

    @Override
    public List<Port> list(Long vendorId, String subnetId) throws Exception {
        String sql = "select * from port where vendor_id = :vendorId and subnet_id = :subnetId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("subnetId", subnetId);
        List<Object> list = this.queryForList(sql, params, Port.class);
        List<Port> ports = new ArrayList<>();
        Port port = null;
        for (Object obj : list) {
            port = (Port) obj;
            ports.add(port);
        }
        return ports;
    }

    @Override
    public List<Port> listByVid(Long vendorId) throws Exception {
        String sql = "select * from port where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        List<Object> list = this.queryForList(sql, params, Port.class);
        List<Port> ports = new ArrayList<>();
        Port port = null;
        for (Object obj : list) {
            port = (Port) obj;
            ports.add(port);
        }
        return ports;
    }

    @Override
    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from port where 1=1";
        sql = SQLHelper.buildSql(sql, params, null, "");
        Map<String, Object> paramMap = SQLHelper.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    @Override
    public Port save(Port port) throws Exception {
        return (Port) this.saveEntity(port);
    }

    @Override
    public boolean delete(Long id) throws Exception {
        String sql = "delete from port where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    @Override
    public boolean removeByVid(Long vendorId) throws Exception {
        String sql = "delete from port where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.update(sql, params) > 0;
    }

    @Override
    public Port query(Long id) throws Exception {
        String sql = "select * from port where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<Object> list = this.queryForList(sql, params, Port.class);
        if (list.isEmpty()) {
            return null;
        }
        return (Port) list.get(0);
    }

    @Override
    public boolean update(Port port) throws Exception {
        return this.updateEntity(port);
    }

    @Override
    public boolean removeByDeviceId(String deviceId) throws Exception {
        String sql = "delete from port where device_id = :deviceId";
        Map<String, Object> params = MapTools.simpleMap("deviceId", deviceId);
        return this.update(sql, params) > 0;
    }
}
